//
//  LeverBarObject.m
//  StarBugs
//
//  Created by ?? ? on 6/12/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/LeverBarObject.h"
#include "Util/ImageManager.h"


LeverBarObject::~LeverBarObject()
{
    leverArray->removeAllObjects();
    leverArray->release();
    leverArray = NULL;
}

LeverBarObject::LeverBarObject(int type)
{
    mainSprite = new CCSprite;
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
//    CCSprite *sprite;
    leverArray = CCArray::create();
    leverArray->retain();
    
    if(type == objectType_leverBarBlue)
    {
        lever = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "lever_bluebar_1");
        lever->setAnchorPoint(ccp((lever->getContentSize().width - 9 )/ lever->getContentSize().width, 0.5));
        mainSprite->addChild(lever);
        
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_2"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_3"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_4"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_5"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_6"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_7"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_8"));
    }
    else {
        lever = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "lever_redbar_1");
        lever->setAnchorPoint(ccp((lever->getContentSize().width - 9 ) / lever->getContentSize().width, 0.5));
        mainSprite->addChild(lever);
        
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_2"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_3"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_4"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_5"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_6"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_7"));
        leverArray->addObject(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_8"));
    }
    objectType = type;
}

void LeverBarObject::setBody(b2World *_world)
{
    b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;
//	bodyDef.userData = mainSprite;
    bodyDef.position.Set((position.x) / PTM_RATIO, position.y / PTM_RATIO);
	mainBody = _world->CreateBody(&bodyDef);
	
    b2PolygonShape poly;
    b2Vec2 vertices[] = 
    {
        b2Vec2(0/PTM_RATIO, 5/PTM_RATIO), b2Vec2(-60/PTM_RATIO, 5/PTM_RATIO), b2Vec2(-60/PTM_RATIO, -5/PTM_RATIO), b2Vec2(0/PTM_RATIO, -5/PTM_RATIO), 
    };
    
    poly.Set(vertices, 4);
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &poly;	
    fixtureDef.restitution = 0.5f;
	mainBody->CreateFixture(&fixtureDef);
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
    mainSprite->setPosition(position);
    mainSprite->setRotation(-bodyAngle);
    worldTmp = _world;
}

void LeverBarObject::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainBody->SetTransform(mainBody->GetPosition(), CC_DEGREES_TO_RADIANS(bodyAngle));
    mainSprite->setRotation(-bodyAngle);
}

void LeverBarObject::removeBody(b2World *_world)
{
    if(mainBody)
        _world->DestroyBody(mainBody);
}

void LeverBarObject::setMove(cocos2d::CCPoint pt)
{
    position = pt;
    if(mainBody)
        mainBody->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
    mainSprite->setPosition(position);
    mainSprite->setRotation(-bodyAngle);
}

void LeverBarObject::resetObstacle()
{
    if(objectType == objectType_leverBarBlue)
        lever->setDisplayFrame(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_bluebar_1"));
    else
        lever->setDisplayFrame(ImageManager::getFrameWithXml2(kImagesheet_GameSource, "lever_redbar_1"));
    lever->setAnchorPoint(ccp((lever->getContentSize().width - 9 ) / lever->getContentSize().width, 0.5));
    
    if(mainBody == NULL)
        setBody(worldTmp);
}

void LeverBarObject::startOpen(b2World *_world)
{
    leverCnt = 0;
    schedule(schedule_selector(LeverBarObject::tick_open), 0.06);
//    worldTmp = _world;
}

void LeverBarObject::tick_open(float dt)
{
    if(leverCnt == 7)
    {
        unschedule(schedule_selector(LeverBarObject::tick_open));
        return;
    }
    else if(leverCnt == 3)
    {
        worldTmp->DestroyBody(mainBody);
        mainBody = NULL;
    }
    
    lever->setDisplayFrame((CCSpriteFrame *)leverArray->objectAtIndex(leverCnt));
    leverCnt ++;
}
